package com.situ.scientific.servlet;

import com.situ.scientific.pojo.Examine;
import com.situ.scientific.service.IExamineService;
import com.situ.scientific.service.impl.ExamineserviceImpl;
import com.situ.scientific.util.JSONUtil;
import com.situ.scientific.util.MyBeanUtil;
import com.situ.scientific.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/examine")
public class ExamineServlet extends HttpServlet {
    IExamineService examineService = new ExamineserviceImpl();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            /*case "selectByPage":
                selectByPage(req, resp);
                break;*/
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
           /* case "add":
                add(req, resp);
                break;*/
            /*case "selectById":
                selectById(req,resp);
                break;*/
            case "update":
                update(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
        }
    }


    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ExamineServlet.deleteAll");
        //传过来的是一个id的数组
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(ids);
        //收到了传过来的id值，然后调用service
        examineService.deleteAll(ids);
        Result result = new Result(0, "删除成功");
        JSONUtil.toJSON(resp, result);
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ExamineServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        examineService.updateStatus(id, Integer.parseInt(status));
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ExamineServlet.update");
        //Examine examine = MyBeanUtil.copyToBean(req, Examine.class);
        String id = req.getParameter("id");
        String commit = req.getParameter("commit");
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        map.put("commit",commit);
        examineService.update(map);
        Result result = new Result(0, "修改成功");
        JSONUtil.toJSON(resp, result);
    }

    /*private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ExamineServlet.selectById");
        String id = req.getParameter("id");
        List<ExamineType> list = examineTypeService.selectAll();
        Examine examine = examineService.selectById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("examine",examine);
        System.out.println("servlet中:"+examine+"and list:"+list);
        JSONUtil.toJSON(resp,Result.ok(map));

    }*/

   /* private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ExamineServlet.add");
        String name = req.getParameter("name");
        System.out.println(name);
        //封装一下

        Examine examine = MyBeanUtil.copyToBean(req, Examine.class);
        System.out.println(examine);
        examineService.add(examine);
        //返回Ajax数据
        Result result = new Result(0, "添加成功");
        JSONUtil.toJSON(resp, result);
    }*/


    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("ExamineServlet.deleteById");
        //接收参数
        String id = req.getParameter("id");
        System.out.println("id:" + id);
        //调用函数
        int count = examineService.deleteById(Integer.parseInt(id));//没有返回值
        Result result = null;
        if (count > 0) {
            result = new Result(0, "删除成功");
        } else {
            result = new Result(1, "删除失败");
        }
        //固定的三行
        JSONUtil.toJSON(resp, result);
        //*resp.setContentType("text/html;charset=UTF-8");
        //ObjectMapper mapper = new ObjectMapper();
        //mapper.writeValue(resp.getWriter(), result);//*//现在不知道这里要返回什么
    }

   /* private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("ExamineServlet.selectByPage");
        *//*String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");*//*
        //1.封装所需数据page:当前页码 ;limit:每页的信息数 ;name:查找的课程名字 ;credit:查找的学分

        //调用位于service层的分页查找功能
        ExamineQuery examineQuery = MyBeanUtil.copyToBean(req, ExamineQuery.class);
        System.out.println(examineQuery);
        PageRusult pageResult = examineService.selectByPage(examineQuery);

        JSONUtil.toJSON(resp, pageResult);

    }
*/
}